{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from itertools import combinations \n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# should take less than 1 min\n",
    "ddi = pd.read_csv('./DDI/ChCh-Miner_durgbank-chem-chem.tsv', sep = '\\t', header = None)\n",
    "pd.DataFrame(list(set(ddi[0].tolist() + ddi[1].tolist()))).rename(columns={0: \"Drug1_ID\"}).to_csv('./DDI/ddi_unique_smiles.csv')\n",
    "drug_list = pd.DataFrame(list(set(ddi[0].tolist() + ddi[1].tolist()))).rename(columns={0: \"Drug1_ID\"})\n",
    "comb = combinations(list(drug_list.values.T[0]), 2)\n",
    "comb = list(comb)\n",
    "pos = [(i[0], i[1]) for i in (ddi.values)]\n",
    "neg = list(set(comb) - set(pos))\n",
    "\n",
    "comb_flipped = [(i[1], i[0]) for i in comb]\n",
    "neg_2 = list(set(comb_flipped) - set(pos))\n",
    "neg_2 = [(i[1], i[0]) for i in neg_2]\n",
    "\n",
    "neg_final = list(set(neg) & set(neg_2))\n",
    "\n",
    "random.seed(a = 1)\n",
    "# adjust negative ratio here.\n",
    "neg_sample = random.sample(neg_final, len(ddi))\n",
    "\n",
    "df = pd.DataFrame(pos+neg_sample)\n",
    "df['label'] = np.array([1]*len(pos) + [0]*len(neg_sample))\n",
    "\n",
    "df = df.rename({0:'Drug1_ID', 1:'Drug2_ID'}, axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_fold(df, x):\n",
    "    test = df.sample(frac = 0.2, replace = False, random_state = x)\n",
    "    train_val = df[~df.index.isin(test.index)]\n",
    "    val = train_val.sample(frac = 0.125, replace = False, random_state = 1)\n",
    "    train = train_val[~train_val.index.isin(val.index)]\n",
    "    \n",
    "    path = './DDI/fold'+str(x)\n",
    "    train.reset_index(drop = True).to_csv(path + '/train.csv')\n",
    "    val.reset_index(drop = True).to_csv(path + '/val.csv')\n",
    "    test.reset_index(drop = True).to_csv(path + '/test.csv')\n",
    "    \n",
    "    return train, val, test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "fold_n = 1\n",
    "#!mkdir './DDI/fold{fold_n}'\n",
    "# uncommand the above line, if fold_n is not 1, since I have already created fold_1\n",
    "train, val, test = create_fold(df, fold_n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
